package org.dl.gjz.menyj.controller;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class CommonContrller {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    /**
     *启动流程实例
     *  127.0.0.1:8088/server1/start/Process_pool_lane
     */
    @GetMapping("/start/{processKey}")
    public String startProcess(@PathVariable(value = "processKey") String processKey) {
        log.info("Process: {} start", processKey);
        identityService.setAuthentication("xiaoming", null);
        VariableMap variableMap = Variables.createVariables();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, variableMap);
        identityService.clearAuthentication(); // 3. 清除认证
        return processKey + " is Success";
    }
}
